#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
//using namespace std;
#define NUM 16
int main(int argc,char* argv[],char*env[])
{
	char *const _env[NUM] = {(char*)"MY_VAL=23456677",NULL};
	pid_t ret = fork();
	if(ret == 0)//子进程
	{
		printf("子进程开始运行,pid:%d\n",getpid());
		sleep(1);
		//execl("/usr/bin/ls","ls","-1",NULL);
		
		//execlp("ls","ls","-1",NULL);
		
		//char *const _argv[16] = {(char*)"ls",(char*)"-a",(char*)"-l",NULL};		
		//execv("/usr/bin/ls",_argv);
		
		
		//char *const _argv[16] = {(char*)"ls",(char*)"-a",(char*)"-l",NULL};		
		//execvp("ls",_argv);
		
		//替换成自己写的进程
		//execl("./mycmd","mycmd","-a","-b",NULL);
		//execle("./mycmd","mycmd","-a",NULL,_env);
		execle("./mycmd","mycmd","-a",NULL,env);
		exit(2);

	}
	else//父进程
	{
		printf("父进程开始等待\n");
		int status = 0;
		pid_t res = waitpid(-1,&status,0);//0是阻塞等待的意思
		if(res>0)
		{
			//等待成功
			printf("等待成功，子进程结束码是:%d\n",WEXITSTATUS(status));
		}
		else
		{
			printf("等待失败，子进程状态异常");
		}
	}
	return 0;
}
